package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.pojo.Affix;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.pojo.RouteExample;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.RouteService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class RouteServiceImpl implements RouteService {

    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private AffixService affixService;

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 根据id查询线路详情
     *
     * @return
     */
    @Override
    public RouteVo findRouteById(RouteVo routeVo) {
        // 查询缓存
        String key = RedisConstant.ROUTESERVICE_FINDROUTEBYID + routeVo.getId();
        RBucket<RouteVo> bucket = redissonClient.getBucket(key);
        // 获取缓存数据
        RouteVo resultRouteVo = bucket.get();
        // 判断是否为空
        if (!EmptyUtil.isNullOrEmpty(resultRouteVo)) {
            return resultRouteVo;
        }
        // 查询mysql
        Route route = routeMapper.selectByPrimaryKey(routeVo.getId());
        // 查询线路包含的图片信息
        AffixVo affixVo = new AffixVo();
        affixVo.setBusinessId(route.getId());
        List<AffixVo> affixVoList = affixService.findAffixByBusinessId(affixVo);
        // 将线路包含的图片集合封装到线路对象中
        resultRouteVo = BeanConv.toBean(route, RouteVo.class);
        resultRouteVo.setAffixVoList(affixVoList);
        // 判断查询结果,并存放到redis中
        if (!EmptyUtil.isNullOrEmpty(resultRouteVo)) {
            bucket.set(resultRouteVo, 3600, TimeUnit.SECONDS);
        }
        return resultRouteVo;
    }

    /**
     * 根据条件分页查询线路信息
     *
     * @return
     */
    @Override
    public PageInfo<RouteVo> findRouteByPage(RouteVo routeVo, int pageNum, int pageSize) {
        // 将pageNum和pageSize存放到当前线程上
        PageHelper.startPage(pageNum, pageSize);
        // 拼接查询条件
        RouteExample example = new RouteExample();
        RouteExample.Criteria criteria = example.createCriteria();
        //多条件查询
        // 根据分类id查询分类下的线路
        if (!EmptyUtil.isNullOrEmpty(routeVo.getCategoryId())) {
            criteria.andCategoryIdEqualTo(routeVo.getCategoryId());
        }
        // 根据线路名称模糊查询
        if (!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())) {
            criteria.andRouteNameLike("%" + routeVo.getRouteName() + "%");
        }
        if (!EmptyUtil.isNullOrEmpty(routeVo.getMinPrice())) {
            criteria.andPriceGreaterThan(routeVo.getMinPrice());
        }
        if (!EmptyUtil.isNullOrEmpty(routeVo.getMaxPrice())) {
            criteria.andPriceLessThan(routeVo.getMaxPrice());
        }
        //注意这里的字段为数据库字段：类似example.setOrderByClause("index ASC,created_time ASC");
        // 首页上 人气旅游 主题旅游 最新旅游 条件
        // 最新旅游
        if (!EmptyUtil.isNullOrEmpty(routeVo.getOrderNewest())) {
            example.setOrderByClause("created_time desc");
        }
        // 人气旅游
        if (!EmptyUtil.isNullOrEmpty(routeVo.getOrderPopularity())) {
            example.setOrderByClause("attention_count asc");
        }
        List<Route> routeList = routeMapper.selectByExample(example);
        // 立即封装查询结果
        PageInfo<Route> pageInfo = new PageInfo<>(routeList);
        // 定义返回值pageInfo
        PageInfo<RouteVo> resultPageInfo = new PageInfo<>();
        // 拷贝基本类型数据
        BeanConv.toBean(pageInfo, resultPageInfo);
        // 拷贝list集合数据
        List<RouteVo> routeVoList = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);
        resultPageInfo.setList(routeVoList);
        //遍历查询到的线路集合,根据线路id查询线路对应的图片信息
        for (RouteVo routeVoHandler : routeVoList) {
            AffixVo affixVo = AffixVo.builder().businessId(routeVoHandler.getId()).build();
            routeVoHandler.setAffixVoList(affixService.findAffixByBusinessId(affixVo));
        }
        return resultPageInfo;
    }
}